package com.sz.app.edi.rest.api;

import com.sz.app.edi.base.entity.SynCompanyAccount;
import com.sz.app.edi.base.service.SynCompanyAccountService;
import com.sz.app.edi.rest.api.dto.EdiUserAccountDto;
import com.sz.app.edi.rest.api.dto.EdiUserInfo;
import com.sz.biz.common.customer.entity.CusCustomer;
import com.sz.biz.common.customer.entity.CusUser;
import com.sz.biz.common.customer.service.CustomerService;
import com.sz.biz.common.customer.service.CustomerUserService;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Function: 账号管理. <br>
 * Date: 2017年12月25日 16:26 <br>
 * Author: wentao.chang
 */
@RestController
@RequestMapping("${api}/accounts")
@Api(description = " ", tags = "004、账号管理")
public class ApiSynCompanyController extends EdiBaseController {

    @Autowired
    private SynCompanyAccountService synCompanyAccountService;


    @ApiOperation(value = "获取账号的注册信息", notes = "获取账号的注册信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = " \"token\":\"4512f247-d446-4523-b249-d7a6f7d092ae\"",
                    required = true, dataType = "String", paramType = "header")})
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public ResultDto getAccountInfo(@RequestHeader String token) {
        checkLogin(token);
        EdiUserAccountDto accountDto = getUserInfoByToken(token);
        EdiUserInfo ediUserInfo = new EdiUserInfo();
        ediUserInfo.setAccountType(accountDto.getAccountType());
        ediUserInfo.setUserName(accountDto.getUserName());
        ediUserInfo.setIsMasterAccount(accountDto.getIsMasterAccount());
        if (accountDto.getAccountType() == CUS_ACCOUNT) {
            CusCustomer cusCustomer = customerService.findSimpleById(accountDto.getCustomerId());
            ediUserInfo.setCustomerName(cusCustomer.getUserName());
        }
        //TODO 供应商的接口
        if (accountDto.getAccountType() == SUP_ACCOUNT) {

        }
        SynCompanyAccount sca = synCompanyAccountService.findByBizAccountIdAndType(accountDto.getAccountType(),
                accountDto.getCustomerId(), accountDto.getUserId());

        if (sca != null) {
            ediUserInfo.setApiKey(sca.getApiKey());
            ediUserInfo.setSecurityKey(sca.getSecurityKey());
            ediUserInfo.setToken(sca.getToken());
            ediUserInfo.setExpireTime(DateUtils.formatDetailDate(sca.getExpireTime()));
        }
        ResultDto result = new ResultDto();
        result.setData(ediUserInfo);
        return result;
    }

    @ApiOperation(value = "重新生成或者初次生成秘钥信息", notes = "重新生成或者初次生成秘钥信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = " \"token\":\"4512f247-d446-4523-b249-d7a6f7d092ae\"",
                    required = true, dataType = "String", paramType = "header")})
    @RequestMapping(value = "/security", method = {RequestMethod.POST})
    public ResultDto genApiKeyInfo(@RequestHeader String token) {
        EdiUserAccountDto accountDto = getUserInfoByToken(token);
        SynCompanyAccount sca = synCompanyAccountService.findByBizAccountIdAndType(accountDto.getAccountType(),
                accountDto.getCustomerId(), accountDto.getUserId());
        if(sca==null){
            synCompanyAccountService.registryEdiAccount(accountDto.getAccountType(),
                    accountDto.getCustomerId(), accountDto.getUserId());
        }else{
            synCompanyAccountService.reRegistryEdiAccount(sca);
        }
        return getAccountInfo(token);
    }
}
